package com.lyrcsoft.rxjava1x.operators.filter;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fujc2dev@126.com
 * @date 2024-11-21
 */
public class distinctUntilChanged {
    public static void main(String[] args) {
        /**
         * distinctUntilChanged()和distinct()类似，只不过它判定的是Observable发射的当前数据项和前一个数据项是否相同。
         */
        Observable.just(2, 1, 2, 2, 3, 4, 3, 4, 5, 5)
                .distinctUntilChanged()
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer i) {
                        System.out.print(i + " ");
                    }
                });

        /**
         * distinctUntilChanged(Func1)和distinct(Func1)一样，根据Func1中call方法产生一个Key来判断两个相邻的数据项是否相同。
         */
        System.out.println("");
        System.out.println("==============================================================");
        List<House> houses = new ArrayList<>();
        houses.add(new House("中粮·海景壹号", "中粮海景壹号新出大平层！总价4500W起"));
        houses.add(new House("竹园新村", "满五唯一，黄金地段"));
        houses.add(new House("竹园新村", "一楼自带小花园"));
        houses.add(new House("中粮·海景壹号", "毗邻汤臣一品"));
        houses.add(new House("中粮·海景壹号", "顶级住宅，给您总统般尊贵体验"));
        houses.add(new House("竹园新村", "顶层户型，两室一厅"));
        houses.add(new House("中粮·海景壹号", "南北通透，豪华五房"));
        Observable.from(houses)
                .distinctUntilChanged(new Func1<House, String>() {

                    @Override
                    public String call(House house) {
                        return house.communityName;
                    }
                }).subscribe(new Action1<House>() {
                    @Override
                    public void call(House house) {
                        System.out.println("小区:" + house.communityName + "; 房源描述:" + house.desc);
                    }
                });
    }
}
